<?php
namespace App\Http\Service;

use Illuminate\Http\Request;
use App\Http\Service\CommentService;
use App\Models\LoginDetaile;

class IndexService
{
	private static $_obj;
	private $_comment;

	public static function getInstance()
	{
		if (!self::$_obj instanceof self) {
			self::$_obj = new self();
		}
		return self::$_obj;
	}

	public function __construct()
	{
		$this->_comment = CommentService::getInstance();
	}

	/**
	 *  获取服务器配置
	 * @return [type] [description]
	 */
	public function getServiceData(Request $request)
	{
		// 内存使用情况(字节)
		$data['内存使用情况'] = memory_get_usage().' b';
		//ip地址
		$data['服务器IP地址'] = $_SERVER['SERVER_ADDR'];
		//域名端口号
		$data['域名'] = $_SERVER['HTTP_HOST'];
		$data['端口号'] = $_SERVER["SERVER_PORT"];
		//sessionId
		$data['当前用户SESSIONID'] = $request->session()->getId();
		$data['本文件所在文件夹'] = __FILE__;
		//服务器当前时间
		$data['服务器当前时间'] = date("Y-m-d H:i:s", time());
		//服务器语言
		$data['服务器语言'] = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
		//脚本超时时间
		$data['执行脚本超时时间'] = ini_get("max_execution_time").'m';
		//操作系统
		$data['服务器操作系统'] = PHP_OS;
		//操作系统版本
		$data['服务器操作系统版本'] = $_SERVER ['SERVER_SOFTWARE'];
		//当前用户
		$data['当前系统用户名'] = session('fyback_info')['number'];
		$data['系统所在文件夹'] = dirname($_SERVER['DOCUMENT_ROOT']);
		//服务器主机名
		// $data['addr'] = gethostbyaddr($_SERVER['REMOTE_ADDR']);
		// $data['服务器主机名'] = (empty($_SERVER['HTTP_X_CLIENT_PROTO']) ? 'http://' : $_SERVER['HTTP_X_CLIENT_PROTO'].'://').'127.0.0.1';
		//PHP版本
		$data['PHP版本'] = PHP_VERSION;
		//当前用户ip
		$data['当前用户ip'] = $request->ip();
		//当前用户浏览器
		$data['当前用户浏览器'] = $_SERVER['HTTP_USER_AGENT'];
		return $data;
	}

	////获得访客真实ip
     function Getip(){
       if(!empty($_SERVER["HTTP_CLIENT_IP"])){   
          $ip = $_SERVER["HTTP_CLIENT_IP"];
       }
       if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ //获取代理ip
        $ips = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
       }
       if($ip){
          $ips = array_unshift($ips,$ip); 
       }
        
       $count = count($ips);
       for($i=0;$i<$count;$i++){   
         if(!preg_match("/^(10|172\.16|192\.168)\./i",$ips[$i])){//排除局域网ip
          $ip = $ips[$i];
          break;    
          }  
       }  
       $tip = empty($_SERVER['REMOTE_ADDR']) ? $ip : $_SERVER['REMOTE_ADDR']; 
       if($tip=="127.0.0.1"){ //获得本地真实IP
          return $this->get_onlineip();   
       }else{
          return $tip; 
       }
    }

    public function getLastLoginDetail()
	{
		$id = session('fyback_info')['id'];
		$count = LoginDetaile::where([['adid',$id],['status',1]])->count();
		if ($count>1) {
			$data = LoginDetaile::where([['adid',$id],['status',1]])->orderBy('id','desc')->first()->toArray();
			$lastData = LoginDetaile::where([['adid',$id],['id','<',$data['id']],['status',1]])->first();
			
			$lastData['time'] = date("Y-m-d H:i:s", $lastData['time']);

			return ['data'=>$lastData, 'count'=>$count];
		}else{
			$data = LoginDetaile::where([['adid',$id],['status',1]])->first()->toArray();
			
			$data['time'] = date("Y-m-d H:i:s", $data['time']);
			return ['data'=>$data, 'count'=>1];
		}
	}
}